package com.wql.rabbitmq.consumer;


import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    private static final Logger logger = LoggerFactory.getLogger(Consumer.class);
    private static final String QUEUE_NAME = "FLOW_CONTROL_QUEUE";

    @RabbitListener(queues = QUEUE_NAME)
    public void receiveMessage(String msg, Message message, Channel channel) {
        try {
            //一次最多能处理多少条消息
            channel.basicQos(2);

            logger.info("【Consumer  receiveMessage】接收到消息为:[{}]", msg);
            //acknowledge-mode = manual
            //如果在customer端不确认，消息将一直保存在FLOW_CONTROL_QUEUE队列中
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            logger.info("【Consumer  receiveMessage】接收消息后的处理发生异常", e);
        }
    }
}
